package com.sq.partner.manager.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.IGatewayAppDao;
import com.sq.partner.manager.mybatis.model.GatewayApp;
import com.sq.partner.manager.mybatis.model.TreeDto;
import com.sq.partner.manager.util.DBUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author Kang Zhiqiang
 *
 */
@Repository
public class GatewayAppDaoImpl extends BaseDaoImpl implements IGatewayAppDao {

	@Override
	public List<GatewayApp> queryList() throws Exception {

		List<GatewayApp> appList = this.queryList("queryGatewayAppByMap", new GatewayApp());
		return appList;
	}
	
	@Override
	public void save(GatewayApp app) throws Exception {
		this.add("insertGatewayApp", app);
	}
	
	@Override
	public void update(GatewayApp app) throws Exception {
		this.update("updateGatewayAppNotNull",app);
		
	}

	@Override
	public List<GatewayApp> queryList(GatewayApp app) throws Exception {
		List<GatewayApp> appList = new ArrayList<GatewayApp>();
		appList =  this.queryList("queryGatewayAppByDept", app);
		return appList;
	}

	@Override
	public Page queryPage(Page page, String appId) throws Exception {
		Connection conn = null;
		// 查总条数
		PreparedStatement pstmt = null;
		// 查分页
		PreparedStatement pstmt1 = null;
		ResultSet rs = null;
		
		List params = new ArrayList();
		Page p = null;
		try{
			conn = this.getConnection();
			StringBuffer sqlbase = new StringBuffer();
			sqlbase.append("select appId,appname from t_gateway_app where 1=1");
			
			if (StrUtil.isNotEmpty(appId)) {
				sqlbase.append(" and appid= ? ");
				params.add(appId);
			}
			String sql = sqlbase.toString();
			Long total;
			pstmt = conn.prepareStatement("select count(*) from (" + sql + ")");
			
			for (Object object : params) {
				pstmt.setObject(1, object);
			}
			rs = pstmt.executeQuery();
			rs.next();
			total = rs.getLong(1);

			// 查出分页
			p = new Page(page.getCurrent(), total);
			SqlUtil sqlUtil = new SqlUtil();
			String pagesql = sqlUtil.buildPageSqlForOracle(sql, p)
					.toString();
			pstmt1 = conn.prepareStatement(pagesql);
			for (Object object : params) {
				pstmt1.setObject(1, object);
			}
			rs = pstmt1.executeQuery();
			List list = DBUtil.transRs2List(rs);
			p.setData(list);
		}finally{
			close(rs);
			close(pstmt);
			close(pstmt1);
			close(conn);
		}
		
		return p;
	}

	@Override
	public void delByAppId(String appId) throws Exception {
		this.delete("delGatewayAppById", appId);
	}

	@Override
	public GatewayApp queryByApp(GatewayApp app) throws SQLException {
		GatewayApp appfind = (GatewayApp) this.query("queryGatewayAppByMap", app);
		return appfind;
	}

	@Override
	public void updateAppName(GatewayApp app) throws Exception {

		this.update("updateGatewayApp", app);
	}
	
	@Override
	public List<TreeDto> querySubChannelTreeByParentChannelCode(String parentChannelCode) throws Exception{
		return queryList("querySubChannelTreeByParentChannelCode", parentChannelCode);
	}

	@Override
	public List<GatewayApp> loadApplicationData() throws Exception {
		// TODO Auto-generated method stub
		return queryList("queryApplicationData","");
	}

	
}
